package thread;

import java.io.*;
import java.nio.charset.StandardCharsets;


public class SomeCopy {
//    有3个线程都往note.txt中写数据，要求线程1先写入t1-1,t1-2,t1-3,t1-4,t1-5,
//    ，然后线程2写入t2-6到t2-10,，再由线程3写入t3-11到t3-15,，
//    依此规律直到写到t3-75结束，12分。如果代码能够在次基础上具备扩展性，
//    比如可以是2个线程去写、1次写6个数、写到58结束等等，线程数、次数、
//    结束数字都可以自定义扩展的话，额外加分4分。
    static int count=1;

    public static void main(String[] args) throws IOException, InterruptedException {

        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                while (count<=58){
                    write();
                }

            }
        };
        Thread t2 = new Thread("t2"){
            @Override
            public void run() {
                while (count<=58){
                    write();
                }
            }
        };
        Thread t3 = new Thread("t3"){
            @Override
            public void run() {
                while (count<=75){
                    write();
                }
            }
        };

        t3.start();
        t2.start();
        t1.start();

    }
    public static void write()  {

        PrintWriter pw = null;
        try {
            FileOutputStream fos = new FileOutputStream("note.txt", true);
            OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            pw = new PrintWriter(bw,true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        synchronized(SomeCopy.class){
            int m = count;
            for (int i= count;i<m+5;i++){
                if (count<=75){
                    count++;
                    Thread thread = Thread.currentThread();
                    pw.println(thread.getName()+"-"+i);
                }else break;

            }
        }
    }


}



